//
//  AddressBookV2.m
//  OC08_Category_Extension_Protocol
//
//  Created by Scott on 16/3/7.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "AddressBookV2.h"
#import "Contact.h"

@interface AddressBookV2 ()

@property (nonatomic, retain) NSMutableDictionary *mDic;

@end

@implementation AddressBookV2

- (void)dealloc{
    [_mDic release];
    [super dealloc];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.mDic = [NSMutableDictionary dictionary];
    }
    return self;
}


- (NSString *)firstCharacter:(NSString *)name {
    
    return [[name substringToIndex:1] uppercaseString];
    
}

- (void)addContact:(Contact *)con {
    
    // 获取首字母. 调用上面实现的方法.
    NSString *group = [self firstCharacter:con.name];
    
    NSMutableArray *arrOfCont = [self.mDic objectForKey:group];
    
    if (arrOfCont == nil) {
        
        arrOfCont = [NSMutableArray array];
        [arrOfCont addObject:con];
        [self.mDic setObject:arrOfCont forKey:group];
    } else {
        [arrOfCont addObject:con];
    }

}





@end






